package com.anlogic.sdk.bootimage.model;

import com.anlogic.sdk.bootimage.model.options.BootgenOptionHelper;
import com.anlogic.sdk.bootimage.model.options.EncryptionType;
import com.anlogic.sdk.bootimage.model.options.IBootgenOption;
import com.anlogic.sdk.bootimage.model.options.PartionType;
import com.anlogic.sdk.utils.options.ChipType;

import java.util.List;

public class PartitionStringConverter implements IBifParseConstants {

	public static String convertToString(Partition partition) {
		StringBuilder builder = new StringBuilder();
		builder.append("\t");
		builder.append("[");
		if (partition.getPartitionType() != PartionType.DATAFILE) {
			builder.append(partition.getPartitionType().getId());
			builder.append(getWriteFieldDelimiter());
		}
		List<String> properties = Partition.getProperties();
		for (String property : properties) {
			if (canStoreProperty(partition, property)) {
				writeProperty(partition, builder, property);
			}
		}
		builder.delete(builder.length() - getWriteFieldDelimiter().length(), builder.length());
		builder.append("]");
		builder.append(String.valueOf(partition.getFilePath()) + "\n");
		String partitionString = builder.toString();
		return partitionString;
	}

	private static String getWriteFieldDelimiter() {
		return ", ";
	}

	private static boolean canStoreProperty(Partition partition, String property) {
		if (!partition.getPartitionType().supportsProperties())
			return false;
		if (!isValidProperty(partition, property)) {
			return false;
		}
		boolean valueModified = !partition.hasDefaultValue(property);
		boolean valueExist = partition.hasValue(property);
		return (valueExist || valueModified);
	}
	
	private static boolean isValidProperty(Partition partition, String property) {
		if (partition.getEncryptionType().equals(EncryptionType.NONE) && (property.equals("aeskeyfile") || property.equals("sm4keyfile"))) {
			return false;
		}
		if (partition.getEncryptionType().equals(EncryptionType.AES) && property.equals("sm4keyfile")) {
			return false;
		}
		if (partition.getEncryptionType().equals(EncryptionType.SM4) && property.equals("aeskeyfile")) {
			return false;
		}
		return true;
	}

	private static void writeProperty(Partition partition, StringBuilder builder, String property) {
		boolean hasKey = !Partition.isNoKeyProperty(property);
		String value = partition.getPropertyValue(property);
		if (hasKey) {
			builder.append(property);
			builder.append(" = ");
			builder.append(value);
		} else if (value.equals(property)) {
			builder.append(property);
		}
		builder.append(getWriteFieldDelimiter());
	}

	public static Partition convertToPartition(String partitionString, ChipType chipType) {
		String filePath;
		Partition partition = Partition.defaultPartition(chipType);
		int braceStart = partitionString.indexOf("[");
		int braceEnd = partitionString.indexOf("]");
		boolean hasAttributes = (braceStart != -1 && braceEnd != -1);
		if (hasAttributes) {
			filePath = partitionString.substring(braceEnd + 1).trim();
			String attribString = partitionString.substring(braceStart + 1, braceEnd);
			parseAttributes(partition, attribString);
		} else {
			filePath = partitionString.trim();
		}
		partition.setFilePath(filePath);
		return partition;
	}

	private static void parseAttributes(Partition partition, String attribString) {
		byte b;
		int i;
		String[] arrayOfString;
		for (i = (arrayOfString = attribString.split(",")).length, b = 0; b < i;) {
			String attr = arrayOfString[b];
			String[] split = attr.split("=");
			if (split.length == 2) {
				String property = split[0].trim();
				String value = split[1].trim();
				partition.setPropertyValue(property, value);
			} else if (split.length == 1) {
				String property = split[0].trim();
				if (Partition.isNoKeyProperty(property)) {
					partition.setPropertyValue(property, property);
				} else {
					PartionType partitionType = (PartionType) BootgenOptionHelper
							.getById((IBootgenOption[]) PartionType.values(), property);
					if (partitionType != null)
						partition.setPartitionType(partitionType);
				}
			}
			b++;
		}
	}
}
